/**
  SUNNY project, Anyang Normal University, IMP-CAS
  \class TASparseVec
  \brief Serving as a column for sparse matrix, inherited from ROOT main I/O class
   - TTree, which is dedicated to handle large amount of data. Note that one object
  stands for one column in a sparse matrix.
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021/02/10
  \date Last modified: 2022/11/19 by SUN Yazhou
  \copyright 2020-2021 SUN Yazhou
  \copyright SUNNY project, Anyang Normal University, IMP-CAS
*/

#include "config.h" // contains user switch of definition of ROOT and OpenCV
#ifdef _ROOT_
#include "TTree.h"
#endif
#include "TAMBSDTree.h"
#include "TAException.h"

TAMBSDTree::TAMBSDTree(const string &name, const string &title)
    : TATreeCol(name, title){
  CreateBranch();
} // end constructor

TAMBSDTree::TAMBSDTree(const TAMBSDTree &v) : TATreeCol(v){
  CreateBranch();
  CopyFill(&v); // fill t to this
} // end copy ctor

// copy v to this
TAMBSDTree &TAMBSDTree::operator=(const TAMBSDTree &v){
  TATreeCol::operator=(v);

  CreateBranch();
  CopyFill(&v); // fill v to this
  return *this;
} // end assignment function


ull TAMBSDTree::GetMBSDInBit(ull mbsdIndex) const{
  GetEntry(mbsdIndex);
  return fBit;
} // end member function GetMBSDInBit

void TAMBSDTree::Fill(ull bit){
  fBit = bit; Fill();
} // end member function Fill

void TAMBSDTree::CopyFill(const TATreeCol *t){
  Clear();
  if(!t->GetEntries()) return;

  const TAMBSDTree *tt = dynamic_cast<const TAMBSDTree *>(t);
  if(!tt) TAException::Error("TAMBSDTree", "CopyFill: Not a TAMBSDTree pointer");

  const ull n = tt->GetEntries();
  for(ull i = 0; i < n; i++) Fill((*tt)[i]);
} // end member function CopyFill

void TAMBSDTree::CreateBranch(){
#ifdef _ROOT_
  fTree->Branch("bit", &fBit, "bit/l");
#else
  TAException::Error("TAMBSDTree", "CreateBranch: ROOT not found.");
#endif
} // end member function CreateBranch

void TAMBSDTree::SetBranchAddress(){
#ifdef _ROOT_
  // already set
  if((void*)(&fBit) == (void*)fTree->GetBranch("bit")->GetAddress()) return;

  fTree->SetBranchAddress("bit", &fBit);
#else
  TAException::Error("TAMBSDTree", "SetBranchAddress: ROOT not found.");
#endif
} // end member function SetBranchAddress
